<template>
	<view class="changePwd-father">
		<view class="changePwd-son">
			<uni-forms :value="passwordRequest" :rules="rules" ref="form">
				<uni-forms-item label="旧密码" name="oldPwd">
					<uni-easyinput type="password" v-model="passwordRequest.oldPwd" placeholder="请输入旧密码" />
				</uni-forms-item>
				<uni-forms-item label="新密码" name="newPwd">
					<uni-easyinput type="password" v-model="passwordRequest.newPwd" placeholder="请输入新密码" />
				</uni-forms-item>
				<uni-forms-item label="确认新" name="newPwd2">
					<uni-easyinput type="password" v-model="passwordRequest.newPwd2" placeholder="请确认新密码" />
				</uni-forms-item>
				<button @click="updatePwd" type="primary">修改密码</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				passwordRequest: {
					oldPwd: null,
					newPwd: null,
					newPwd2: null
				},
				rules: {
					// 对name字段进行必填验证
					oldPwd: {
						rules: [{
							required: true,
							errorMessage: '旧密码不能为空'
						}]
					},
					newPwd: {
						rules: [{
							required: true,
							errorMessage: '新密码不能为空'
						}]
					},
					newPwd2: {
						rules: [{
							required: true,
							errorMessage: '新密码不能为空'
						}]
					}
				}
			}
		},
		onBackPress(e) {
			// 这里可以自定义返回逻辑，比如下面跳转其他页面
			uni.navigateTo({
				url:'/pages/my/accountManage'
			});
			return true
		},
		methods: {
			updatePwd() {
				this.$refs.form.submit().then(res => {
					if (this.passwordRequest.newPwd !== this.passwordRequest.newPwd2) {
						uni.showToast({
							title: '两次新密码不一致',
							icon: 'none'
						})
						return
					}
					// 发送请求
					this.$http.request({
						url: '/organizations/web/user/changePassword',
						data: this.passwordRequest,
						method: 'POST'
					}).then(res => {
						res = res.data;
						if (res.result) {
							uni.showToast({
								title: res.msg
							})
							this.passwordRequest.oldPwd = null
							this.passwordRequest.newPwd = null
							this.passwordRequest.newPwd2 = null
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style>
	.changePwd-father {
		display: flex;
		justify-content: center;
	}

	.changePwd-son {
		margin-top: 20rpx;
		width: 92%;
	}
</style>
